package com.onlineclass.controller;

import java.util.HashMap;
import java.util.Map;
import com.mvc.annotation.Controller;
import com.mvc.annotation.RequestMapping;
import com.onlineclass.dao.SMSCodeDAO;
import com.onlineclass.util.SendSMSFromRongLian;

@Controller
@RequestMapping("/sms")
public class SendSMS {

	private SMSCodeDAO smsCodeDAO = new SMSCodeDAO();
	
	/**
	 * 发送验证码并保存入库
	 * @param mobile
	 * @return
	 */
	@RequestMapping("/send.do")
	public Map<String, Object> send(String mobile) {
		Map<String, Object> map = new HashMap<String, Object>();
		int nums = (int) (Math.random() * 100000);
		System.out.println(nums);
		String result = SendSMSFromRongLian.send(nums, mobile);
		int code = result.equals("000000")?0:-1; //0为发送成功的标志
		map.put("code", code);
		smsCodeDAO.save(nums, mobile);
		return map;
	}
	
	@RequestMapping("/verify.do")
	public Map<String, Object> verify(String mobile, Integer code) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", (smsCodeDAO.verify(code, mobile)==0)?-1:0);//0位验证码输入的是对的
		return map;
	}
}
